//
//  BaseViewController.m
//  项目一
//
//  Created by 芮京 on 2018/9/28.
//  Copyright © 2018 芮京. All rights reserved.
//

#import "BaseViewController.h"
#import "BaseTableViewCell.h"
#import "BaseModel.h"
#import "NewsdataViewController.h"
@interface BaseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;

@property(nonatomic,strong)NSMutableArray *datasource;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tab];
    
    [self initdata];
    //添加下拉刷新  上拉加载
    [self addheaderrefresh];
    [self addfootrefresh];
    //重新发送网络请求
    [self setMyUrl];
    [self loadData];
    
}
//                 初始化数据
-(void)initdata{
    //初始化数据源r
    self.datasource=[NSMutableArray array];
    //初始化页数
    self.page=1;


}
-(UITableView *)tab{
    if (!_tab) {
        _tab=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tab.delegate=self;
        _tab.dataSource=self;
    }
    //注册单元格
    [_tab registerNib:[UINib nibWithNibName:@"BaseTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    return _tab;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.datasource.count>0) {
        BaseModel *model=self.datasource[indexPath.row];
        [cell loadDataFromModel:model];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

//点击跳转详情界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //去掉选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    //跳转界面
    NewsdataViewController *data=[NewsdataViewController new];
    //隐藏标签栏控制器
    data.hidesBottomBarWhenPushed=YES;
    
    //获取点中单元格的详情URL
    BaseModel *model=self.datasource[indexPath.row];
    
    
    data.urlstring=model.url;
    
    [self.navigationController pushViewController:data animated:YES];
    
}

//                 添加头部刷新
-(void)addheaderrefresh{
    MJRefreshGifHeader *header=[MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        //初始化页数
        self.page=1;
        
        //清空数据源
        [self.datasource removeAllObjects];
        
        //重新发送网络请求
        [self setMyUrl];
        [self loadData];
        
        
    }];
     NSArray * imageArr = @[[UIImage imageNamed:@"common_loading_anne_0"],[UIImage imageNamed:@"common_loading_anne_1"]];
    //添加正在刷新中的状态
    [header setImages:imageArr forState:MJRefreshStateRefreshing];
    //闲置状态
    [header setImages:@[[UIImage imageNamed:@"common_loading_anne_0"]] forState:MJRefreshStateIdle];
    //文字
    [header setTitle:@"马上完成....." forState:MJRefreshStateRefreshing];
    self.tab.mj_header=header;
    
}
//上拉加载
-(void)addfootrefresh{
    MJRefreshAutoGifFooter *footer=[MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
       //页面++
        self.page ++;
        //重新请求数据
        [self setMyUrl];
        [self loadData];
        
        
    }];
    
    NSArray * imageArr = @[[UIImage imageNamed:@"loading_teemo_1"],[UIImage imageNamed:@"loading_teemo_2"]];
    [footer setImages:imageArr forState:MJRefreshStateRefreshing];
    self.tab.mj_footer=footer;
    
}
//设置网址
-(void)setMyUrl{
    self.url=TOUTIAO_PATH;
    self.KEY=TOUTIAO_KEY;
    
}
//请求数据
-(void)loadData{
    //显示加载栏
    [SVProgressHUD showWithStatus:@"加载中..."];
    
    //创建网络请求
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    
    //设置默认请求类型（NSData）
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manger GET:self.url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //解析数据
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *resultall=dic[self.KEY];
        for (NSDictionary *Dic in resultall) {
            //封装model,建立数据模型 存储数据
            BaseModel *model=[[BaseModel alloc] init];
            [model setValuesForKeysWithDictionary:Dic];
            [self.datasource addObject:model];
        }
        //刷新UI
        [self.tab reloadData];
        
        //取消刷新控件
        [self.tab.mj_header endRefreshing];
        [self.tab.mj_footer endRefreshing];
        //去掉加载栏
        [SVProgressHUD dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //提示用户
        
        
        //取消刷新控件
        [self.tab.mj_header endRefreshing];
        [self.tab.mj_footer endRefreshing];
        //去掉加载栏
        [SVProgressHUD dismiss];
    }];
    
    
}


@end
